package com.qf.service.impl;

import com.qf.dao.C_vDao;
import com.qf.dao.CouponDao;
import com.qf.dao.impl.C_vDaoImpl;
import com.qf.dao.impl.CouponDaoImpl;
import com.qf.entity.Coupon;
import com.qf.service.CouponService;
import com.qf.utils.DBUtils;
import com.qf.utils.PageUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class CouponServiceImpl implements CouponService {
    private CouponDao couponDao = new CouponDaoImpl();
    private C_vDao c_vDao = new C_vDaoImpl();

    /**
     * 增加优惠券
     *
     * @param coupon
     * @return
     */
    @Override
    public int addCoupon(Coupon coupon) {
        try {
            return couponDao.addCoupon(coupon);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;

    }

    /**
     * 展示优惠券
     *
     * @param currentPageNoStr
     * @param pageSizeStr
     * @return
     */
    @Override
    public PageUtils<Coupon> getCouponPageUtils(String currentPageNoStr, String pageSizeStr) {
        //处理当前页
        Integer currentPageNo = 1;
        if (currentPageNoStr != null && !"".equals(currentPageNoStr)) {
            currentPageNo = Integer.parseInt(currentPageNoStr);
        }
        //处理页量
        Integer pageSize = 3;
        if (pageSizeStr != null && !"".equals(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        int totalPageCount = 0;
        int totalPageSize = 0;
        List<Coupon> couponList = null;
        try {
            //计算出总记录数
            totalPageCount = couponDao.selectAllCouponCount();
            //计算出总页数
            totalPageSize = totalPageCount % pageSize == 0 ? totalPageCount / pageSize : totalPageCount / pageSize + 1;
            //求出当前页集合
            couponList = couponDao.selectCouponListByPage(currentPageNo, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //实例化一个PageUtils
        PageUtils<Coupon> pageUtils = new PageUtils<>(currentPageNo, pageSize, totalPageCount, totalPageSize, couponList);
        return pageUtils;
    }


    /**
     * 删除优惠券
     *
     * @param cid
     * @return
     */
    @Override
    public int deleteC_vAndCouponByCid(Integer cid) {
        if (cid == null) {
            return -1;
        }
        //用了统计执行sql语句的条数
        int count = 0;
        //开启事务
        Connection con = null;
        //num1是c_v表删除的条数
        //num2是coupon表删除的条数
        int num1 = 0;
        int num2 = 0;
        try {
            con = DBUtils.DB.getQr().getDataSource().getConnection();
            con.setAutoCommit(false);
            //先删除从表也即是c_v表,再删除主表也即是coupon表
            num1 = c_vDao.deleteC_vByCid(cid);
            //如果没有发生发生异常，则
            count++;
            num2 = couponDao.deleteCouponByCid(cid);
            count++;
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        } finally {
            //当sql语句执行的条数为2并且num1>=0并且num2>0，这种情况可以提交,因为有优惠券不一定该优惠券会发放，所以num1有可能是0
            if (count == 2 && num1 >= 0 && num2 > 0) {
                try {
                    con.commit();
                    con.setAutoCommit(true);
                    return num2;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    //当不符合条件的时候就要回滚
                    assert con != null;
                    con.rollback();
                    con.setAutoCommit(true);
                    return -1;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return -1;
    }

}
